package com.rolerealm.common.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;

/**
 * 当Redis不可用时的备用配置
 */
@Slf4j
@Configuration
public class NoRedisConfig {

    @Bean
    @ConditionalOnMissingBean(RedisTemplate.class)
    public RedisTemplate<String, Object> noOpRedisTemplate() {
        log.warn("Redis不可用，使用空操作Redis模板");
        return new NoOpRedisTemplate<>();
    }

    @Bean
    @ConditionalOnMissingBean(StringRedisTemplate.class)
    public StringRedisTemplate noOpStringRedisTemplate() {
        log.warn("Redis不可用，使用空操作StringRedis模板");
        return new NoOpStringRedisTemplate();
    }

    /**
     * 空操作Redis模板
     */
    private static class NoOpRedisTemplate<K, V> extends RedisTemplate<K, V> {
        @Override
        public void afterPropertiesSet() {
            // 空操作
        }
    }

    /**
     * 空操作StringRedis模板
     */
    private static class NoOpStringRedisTemplate extends StringRedisTemplate {
        @Override
        public void afterPropertiesSet() {
            // 空操作
        }
    }
}
